﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BuckleyChurch.Business.Utility;

namespace ChurchData.EBulletin
{
    public partial class EBulletinSetAction : ChurchForm
    {
        private Person _person { get; set; }

        public EBulletinSetAction(Person person)
        {
            InitializeComponent();

            _person = person;

            txtFirstName.Text = person.FirstName;
            txtLastName.Text = person.LastName;
            txtEmail.Text = person.EmailAddress;

            FindPotentialMatches();
        }

        private void FindPotentialMatches()
        {
            UpdateRecord();
            List<Person> partialMatches = BuckleyChurch.Business.ElectronicBulletin.EBulletin.GetPartialMatchSyncRecords(_person);
            grdPeople.DataSource = partialMatches;
            grdPeople.Columns["PeoplePK"].Visible = false;
            grdPeople.Columns["TempSignupPK"].Visible = false;
        }

        private void UpdateRecord()
        {
            _person.FirstName = txtFirstName.Text.Trim();
            _person.LastName = txtLastName.Text.Trim();
            _person.EmailAddress = txtEmail.Text.Trim();
        }

        private void btnLink_Click(object sender, EventArgs e)
        {
            if (grdPeople.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select a row to sync this signup to.", "No selection");
                return;
            }

            UpdateRecord();
            _person.PeoplePK = (int)grdPeople.SelectedRows[0].Cells["PeoplePK"].Value;
            _person.Action = BuckleyChurch.Business.Enumerations.LinkAction.Link;
            Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Are your sure?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                UpdateRecord();
                _person.Action = BuckleyChurch.Business.Enumerations.LinkAction.Delete;
                Close();
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            UpdateRecord();
            _person.Action = BuckleyChurch.Business.Enumerations.LinkAction.New;
            Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FindPotentialMatches();
        }
    }
}